package com.ssm.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ssm.entity.Permission;
import com.ssm.service.PermissionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

/**
 * @Author jie_ge
 * @Date 2022/4/22
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Reference
    private PermissionService permissionService;
    /**
     * 获取菜单
     * @param map
     * @return
     */
    @GetMapping
    public String index(Map map){
        List<Permission> list = permissionService.findAllMenu();
        map.put("list",list);
        return "permission/index";
    }

    /**
     * 添加
     * @param map
     * @param permission
     * @return
     */
    @GetMapping("/create")
    public String create(Map map,Permission permission){
        map.put("permission",permission);
        return "permission/create";
    }

    /**
     * 保存添加
     * @param permission
     * @return
     */
    @PostMapping("/save")
    public String save(Permission permission){
        permissionService.insert(permission);
        return "common/successPage";
    }

    /**
     * 修改
     * @param map
     * @param id
     * @return
     */
    @GetMapping("/edit/{id}")
    public String edit(Map map, @PathVariable("id") Long id){
        Permission permission = permissionService.getById(id);
        map.put("permission",permission);
        return "permission/edit";
    }

    /**
     * 保存修改
     * @param permission
     * @return
     */
    @PostMapping("/upadte")
    public String update(Permission permission){
        permissionService.update(permission);
        return "common/successPage";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id){
        permissionService.deleteById(id);
        return "redirect:/permission";
    }
}
